package com.tz.crm.entity;

import javax.persistence.*;
import java.util.Set;

/**
 * 部门实体类
 */
@Entity
@Table(name = "tbl_department")
public class Department extends BaseEntity {

    private static final long serialVersionUID = 4735907212206380861L;

    private String deptName;//部门名称
    private Department parent;//父部门
    private Set<Department> children;//子部门
    private Set<User> user;//部门所属员工
    private String remark;//备注信息

    /**
     * 空参构造
     */
    public Department() {
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    @ManyToOne
    public Department getParent() {
        return parent;
    }

    public void setParent(Department parent) {
        this.parent = parent;
    }

    @OneToMany(mappedBy = "parent", cascade = {CascadeType.ALL})
    public Set<Department> getChildren() {
        return children;
    }

    public void setChildren(Set<Department> children) {
        this.children = children;
    }

    @OneToMany(mappedBy = "dept", cascade = {CascadeType.ALL})
    public Set<User> getUser() {
        return user;
    }

    public void setUser(Set<User> user) {
        this.user = user;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    /**
     * 将属性拼接到字符串中
     *
     * @return HTML Tags
     */
    public String toTagString() {
        if (this.getChildren() != null && this.getChildren().size() > 0) {
            String pre = "<li id=\"" + this.getId()
                    + "\" class=\"jstree-open\"><a id=\"a_"
                    + this.getId() + "\" href=\"#\">"
                    + this.getDeptName() + "</a>";
            //String middle = "<ul>";
            StringBuffer buffer = new StringBuffer();
            buffer.append("<ul>");
            for (Department d : this.getChildren()) {
                //middle += d.toTagString();
                buffer.append(d.toTagString());
            }
            //middle += "</ul>";
            buffer.append("</ul>");
            String middle = buffer.toString();
            String post = "</li>";
            return pre + middle + post;
        } else {
            return "<li id=\"" + this.getId() + "\"><a id=\"a_"
                    + this.getId() + "\" href=\"#\">" + this.getDeptName() + "</a></li>";
        }
    }

}
